
<?php
include_once('classes/News.php');
include_once('functions/getTrendingWords.php');
include_once('functions/normalizeString.php');

function newsAreRelated($news,$otherNews){
	
	$unwantedWordsArray = array(",",":","\"","." , "!"," en ","Una "," aún "," ante "," es "," lo "," al ","A "," del ","De "," se ","Se "," una "," más ","En "," la ","La "," las ","Las "," los ","Los "," le ","Le "," un ","Un "," que ","Que "," su ","Su "," por "," a "," el ","El "," de "," ellos "," no ","No "," con "," para "," por "," segun "," mas "," entre " , " y "," sus "," así ","Así ");
	$relationshipLimit = 3;
	
	
	$newsTitleWOTags = strip_tags($news->getTitle());
	$otherNewsTitleWOTags = strip_tags($otherNews->getTitle());
	$newsKWs = explode(" ",str_replace($unwantedWordsArray, " ", $newsTitleWOTags));
	$otherNewsKW =  explode(" ",str_replace($unwantedWordsArray, " ", $otherNewsTitleWOTags));
	
	
	$wordsInCommon = wordsInCommon($newsKWs,$otherNewsKW);
	

	if ($wordsInCommon < $relationshipLimit){
		$result = FALSE;
	}
	else{
		$result = TRUE;
	}
	
	
	return ($result);

}

function wordsInCommon($newsKWs,$otherNewsKW){
	$matches = 0;
	foreach ($newsKWs as $nkw){
		if (strlen($nkw) > 2){
			foreach ($otherNewsKW as $onkw){
				$nkw = normalize($nkw);
				$onkw = normalize($onkw);
				$cmp = strcasecmp ($nkw,$onkw);
				if ($cmp == 0){
					$matches++;			
				}
			}
		}
	}
	
	return ($matches);
}
?>
